/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.activities.dashboard;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.view.ViewStub;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.phonebooksharing.android.R;
import com.phonebooksharing.android.activities.media.AudioHandler;
import com.phonebooksharing.android.util.CloseableUtil;
import com.phonebooksharing.dto.friend.ContactItem;
import com.phonebooksharing.dto.msg.TextMessageItem;

//TODO: media player UI components by open intents
public class MessageReaderActivity extends AbstractReaderActivity {

    private AudioHandler mainAudioHandler;
    
    public static final String TITLE           = "ti",
                               TEXT            = "tt",
                               ITEM_OBJ    = "ms";
    
    protected ContactItem contactItem;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        // Handle the calling intent
        final Intent intent = getIntent();
        contactItem = (ContactItem)intent.getSerializableExtra(ITEM_OBJ);

        if (READ_AUDIO.equals(intent.getAction())) {
            ((ViewStub)findViewById(R.id.audioStub)).inflate();
            mainAudioHandler = new AudioHandler(this, getWindow().getDecorView());
            mainAudioHandler.setMedia(intent.getData());
            ((TextView)findViewById(R.id.title)).setText("Voice Message:");
        } else {//text
            View v = ((ViewStub)findViewById(R.id.textStub)).inflate();
            TextMessageItem tm = (TextMessageItem)contactItem;
            ((TextView)v.findViewById(R.id.text1)).setText(tm.title);
            ((TextView)v.findViewById(R.id.text2)).setText(tm.text);
            ((TextView)findViewById(R.id.title)).setText("Text Message:");
        }
        
        ((ImageView)findViewById(android.R.id.icon)).setImageBitmap((Bitmap)intent.getParcelableExtra(FRIEND_ICON));
        ((TextView)findViewById(android.R.id.text1)).setText(contactItem.name);
        ((TextView)findViewById(R.id.text3)).setText(contactItem.status);
        long l;
        if((l = intent.getLongExtra(SENT_WHEN, 0)) > 0) {
            ((TextView)findViewById(android.R.id.text2)).setText("(" + DateUtils.getRelativeTimeSpanString(l,
                    System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE) + ")");
        }
        ((TextView)findViewById(R.id.text4)).setText(contactItem.location);
    }
    
    @Override
    protected void prefillTitle(EditText t) {
        super.prefillTitle(t);
        if(contactItem instanceof TextMessageItem) {
            t.setText("RE: " + ((TextMessageItem)contactItem).title );
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        CloseableUtil.safeClose(mainAudioHandler);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CloseableUtil.safeClose(mainAudioHandler);

        mainAudioHandler = null;
    }
}
